//
//  AppDelegate.swift
//  Uber
//
//  Created by 朱 桓 on 16/1/6.
//  Copyright © 2016年 zhu.huan. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var mapManager:BMKMapManager?
    var isRegister:Bool = true
    var mPush:FxPush?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        mPush = MiPush()
        addMap()
        
        
        if isRegister {
            // 显示启动页面
            showGuidepage()
        } else {
            showHomePage()
        }
        return true
    }

    func applicationWillResignActive(application: UIApplication)
    {
        
    }

    func applicationDidEnterBackground(application: UIApplication)
    {
        
    }

    func applicationWillEnterForeground(application: UIApplication)
    {
        
    }

    func applicationDidBecomeActive(application: UIApplication)
    {
        
    }

    func applicationWillTerminate(application: UIApplication)
    {
       
    }
    /**
    *显示向导界面
    */
    func showGuidepage()
    {
        //设置启动页面
        let page = GuidePage()
        self.window?.rootViewController = page
        self.window?.makeKeyAndVisible()
    }
    
    /**
    *显示主界面
    */
    func showHomePage()
    {
        // 显示主界面
        let page = HomePage();
        let navPage = UINavigationController(rootViewController: page)
        
        self.window?.rootViewController = navPage
        self.window?.makeKeyAndVisible()
    }
    
    /**
    *初始化百度地图管理对象
    */
    func addMap()
    {
        // 要使用百度地图，请先启动BaiduMapManager
        mapManager = BMKMapManager()
        // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
        let ret = mapManager!.start("f6Ki14NpWfH9m0YepyphYbuG", generalDelegate: nil)

        if (!ret) {
            ptLog("manager start failed!")
        }
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        showNotifation(userInfo)
        updateNotification(application)
        mPush?.handleNotification(userInfo)
    }
    
    func showNotifation(info:[NSObject:AnyObject])
    {
        print(info["aps"]?.objectForKey("alert"))
    }
    
    func updateNotification(app:UIApplication)
    {
        app.cancelAllLocalNotifications()
        app.applicationIconBadgeNumber = 0
    }
}

